/*
 * Class: Configuration
 * 
 * Created on Sep 26, 2013
 * 
 * (c) Copyright by Minh Tran
 * Global Cybersoft VN.
 * Floor 3-4-5, Helios Building, Quang Trung Software City
 */
package com.minh.tran.sg;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Configuration
{

    /** The Constant LOG. */
    private static final Log LOG = LogFactory.getLog(Configuration.class);

    /** The download link nct. */
    private String linkNCTFormatSong;

    /** The link nct format playlist. */
    private String linkNCTFormatPlaylist;

    /** The key link nct. */
    private String keyLinkNCT;

    /**
     * Instantiates a new configuration.
     */
    public Configuration()
    {
        this.linkNCTFormatSong = "^(http://)(www.)?nhaccuatui.com/bai-hat/([A-Za-z0-9-]+)\\.([A-Za-z0-9_]+)(.html)$";
        this.linkNCTFormatPlaylist = "^(http://)(www.)?nhaccuatui.com/playlist/([A-Za-z0-9-]+)\\.([A-Za-z0-9_]+)(.html)$";
        this.keyLinkNCT = "http://www.nhaccuatui.com/flash/xml?key";
        this.initialize();
    }

    /**
     * Load.
     */
    public void load()
    {
        this.initialize();
    }

    /**
     * Initialize.
     */
    private void initialize()
    {
        LOG.info("Execute initiate...");
        final List<String> lstString = new ArrayList<String>();
        this.readFile(lstString);
        if (lstString.size() > 0)
        {
            for (String string : lstString)
            {
                String firstChars = string.substring(0, Constant.PREFIX_LENGTH);
                final String link = string.substring(Constant.PREFIX_LENGTH);
                LOG.info(firstChars);
                if (Constant.FMT_NCT_SONG.equals(firstChars))
                {
                    this.linkNCTFormatSong = link;
                    LOG.info(link);
                }
                else if (Constant.FMT_NCT_PLAYLIST.equals(firstChars))
                {
                    this.linkNCTFormatPlaylist = link;
                    LOG.info(link);
                }
                else if (Constant.KEY_NCT.equals(firstChars))
                {
                    this.keyLinkNCT = link;
                    LOG.info(link);
                }
            }
        }
    }

    /**
     * Read file.
     */
    private void readFile(final List<String> lstString)
    {
        try (BufferedReader br = new BufferedReader(new FileReader(Constant.FILE_DIR)))
        {
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null)
            {
                lstString.add(sCurrentLine);
            }
        }
        catch (IOException e)
        {
            LOG.error("Failed to read file!");
        }
    }

    /**
     * Write file.
     *
     * @param content the content
     */
    private void writeFile(final String content)
    {
        try
        {
            final File file = new File(Constant.FILE_DIR);
            if (!file.exists())
            {
                file.createNewFile();
            }
            final FileWriter fw = new FileWriter(file.getAbsoluteFile());
            final BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Save.
     */
    public void save()
    {
        final StringBuilder sb = new StringBuilder();
        sb.append(Constant.FMT_NCT_SONG).append(this.linkNCTFormatSong).append(Constant.NEW_LINE);
        sb.append(Constant.FMT_NCT_PLAYLIST).append(this.linkNCTFormatPlaylist).append(Constant.NEW_LINE);
        sb.append(Constant.KEY_NCT).append(this.keyLinkNCT).append(Constant.NEW_LINE);
        this.writeFile(sb.toString());
        sb.setLength(0);
    }

    /**
     * Gets the download link nct.
     *
     * @return Returns the downloadLinkNCT.
     */
    public synchronized String getLinkNCTFormatSong()
    {
        return this.linkNCTFormatSong;
    }

    /**
     * Sets the download link nct.
     *
     * @param downloadLinkNCT The downloadLinkNCT to set.
     */
    public synchronized void setLinkNCTFormatSong(String downloadLinkNCT)
    {
        this.linkNCTFormatSong = downloadLinkNCT;
    }

    /**
     * @return Returns the downloadLinkPlayList.
     */
    public String getKeyLinkNCT()
    {
        return this.keyLinkNCT;
    }

    /**
     * @param keyLinkNCT The downloadLinkPlayList to set.
     */
    public void setKeyLinkNCT(String keyLinkNCT)
    {
        this.keyLinkNCT = keyLinkNCT;
    }

    /**
     * @return Returns the linkNCTFormatPlaylist.
     */
    public String getLinkNCTFormatPlaylist()
    {
        return this.linkNCTFormatPlaylist;
    }

    /**
     * @param linkNCTFormatPlaylist The linkNCTFormatPlaylist to set.
     */
    public void setLinkNCTFormatPlaylist(String linkNCTFormatPlaylist)
    {
        this.linkNCTFormatPlaylist = linkNCTFormatPlaylist;
    }

}
